package com.yb.guigu.pick.auth.controller;


import com.yb.guigu.pick.auth.bean.vo.request.SysRolePermissionAssignVo;
import com.yb.guigu.pick.auth.service.ISysRoleService;
import com.yb.guigu.pick.core.bean.GuiguResult;
import com.yb.guigu.pick.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-30
 */
@RestController
@RequestMapping("/sysrole")
public class SysRoleController {

    @Autowired
    ISysRoleService sysRoleService;


    @GetMapping("/all")
    public GuiguResult list(){
        return GuiguResult.success(sysRoleService.list());
    }


    /**
     * 查找角色的权限，
     * @param roleId
     * @return
     */
    @GetMapping("/permissions/{roleId}")
    public GuiguResult rolePermission(@PathVariable Integer roleId){
        return sysRoleService.rolePermission(roleId);
    }

    @GetMapping("/routes/{roleId}")
    public GuiguResult roleRoutes(@PathVariable Integer roleId){
        return sysRoleService.roleRoutes(roleId);
    }

    @PostMapping("/permission/assign/{roleId}")
    public GuiguResult assignPermission(@PathVariable Integer roleId,@RequestBody SysRolePermissionAssignVo rolePermissionAssignVo){
        rolePermissionAssignVo.setRoleId(roleId);
        return sysRoleService.assignPermission(roleId,rolePermissionAssignVo);
    }

    /**
     * 取消权限
     * @param roleId
     * @param rolePermissionAssignVo
     * @return
     */
    @PostMapping("/permission/unassign/{roleId}")
    public GuiguResult unAssignPermission(@PathVariable Integer roleId,@RequestBody SysRolePermissionAssignVo rolePermissionAssignVo){
        rolePermissionAssignVo.setRoleId(roleId);
        return sysRoleService.unAssignPermission(roleId,rolePermissionAssignVo);
    }



    @PostMapping("/route/assign/{roleId}")
    public GuiguResult assignRoute(@PathVariable Integer roleId,@RequestBody SysRolePermissionAssignVo rolePermissionAssignVo){
        rolePermissionAssignVo.setRoleId(roleId);
        return sysRoleService.assignRoute(roleId,rolePermissionAssignVo);
    }

    /**
     * 取消权限
     * @param roleId
     * @param rolePermissionAssignVo
     * @return
     */
    @PostMapping("/route/unassign/{roleId}")
    public GuiguResult unAssignRoute(@PathVariable Integer roleId,@RequestBody SysRolePermissionAssignVo rolePermissionAssignVo){
        rolePermissionAssignVo.setRoleId(roleId);
        return sysRoleService.unAssignRoute(roleId,rolePermissionAssignVo);
    }


    public static void main(String[] args) {
        String[] arr=new String[]{"","1,2","","4"};
        System.out.println(StringUtil.join(arr));
        System.out.println();
    }
}
